package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.AtUserEntity;
import com.zw.yzk.kabu.data.network.task.AthUserTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.view.view.AtUserView;

import javax.inject.Inject;
import javax.inject.Named;


/**
 * Created by zhanwei on 2017/7/3.
 */

public class AtUserPresenter implements Presenter {

    private UseCase searchUser;
    private AppCompatActivity activity;
    private AtUserView view;

    @Inject
    public AtUserPresenter(AppCompatActivity activity, @Named("atUserUseCase")UseCase searchUser) {
        this.searchUser = searchUser;
        this.activity = activity;
    }

    public void setView(AtUserView view) {
        this.view = view;
    }

    public void searUser(String key, int start) {
        searchUser.execute(new SearchUserSubscriber(activity), new AthUserTask(start, key));
    }

    private class SearchUserSubscriber extends BaseSubscriber<AtUserEntity> {

        public SearchUserSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(AtUserEntity entity) {
            super.onNext(entity);
            view.searchUserSussess(entity.result.uList);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            view.searchFailed();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.search_exhibition_error);
        }
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        searchUser.unSubscribe();
    }
}
